package com.emergency.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.emergency.constant.DepartmentTypeConstant;
import com.emergency.data.entity.Department;
import com.emergency.data.entity.DepartmentLinkman;
import com.emergency.data.vo.DepartmentLinkmanVO;
import com.emergency.data.vo.DepartmentVO;
import com.emergency.mapper.DepartmentLinkmanMapper;
import com.emergency.mapper.DepartmentMapper;
import com.emergency.service.DepartmentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Department)服务实现类
 *
 * @author makejava
 * @since 2020-09-18
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Resource
    private DepartmentMapper departmentMapper;

    @Resource
    private DepartmentLinkmanMapper departmentLinkmanMapper;

    @Override
    public List<DepartmentVO> getDutiesList(Integer type) {
        //查询条件
        QueryWrapper<Department> query = new QueryWrapper<>();
        if (type != null) {
            query.eq("type", type);
        }
        return departmentMapper.selectList(query).stream().map(c -> {
            DepartmentVO vo = new DepartmentVO();
            BeanUtil.copyProperties(c, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<DepartmentVO> getDepartmentList(Integer level) {
        //查询条件
        QueryWrapper<Department> query = new QueryWrapper<>();
        query.eq("type", DepartmentTypeConstant.ADDRESS_BOOK_TYPE);
        if (level != null) {
            query.eq("level", level);
        }
        return departmentMapper.selectList(query).stream().map(c -> {
            DepartmentVO vo = new DepartmentVO();
            BeanUtil.copyProperties(c, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<DepartmentLinkmanVO> getDepartmentLinkmanList(Long id) {
        return departmentLinkmanMapper.selectList(new QueryWrapper<DepartmentLinkman>()
                .eq("dept_id", id)).stream().map(c -> {
                    DepartmentLinkmanVO vo = new DepartmentLinkmanVO();
                    BeanUtil.copyProperties(c, vo);
                    return vo;
        }).collect(Collectors.toList());
    }
}
